
package com.showcai.controller.media;

import com.showcai.controller.base.BaseController;
import com.showcai.module.Media;
import com.showcai.module.MediaClassify;
import com.showcai.result.ResultObj;
import com.showcai.service.AdvertService;
import com.showcai.service.MediaClassifyService;
import com.showcai.service.MediaService;
import com.showcai.tools.PushTools;
import com.showcai.tools.json.JsonHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/media")
@Controller
public class MediaController extends BaseController {
    @Autowired
    MediaService mediaService;
    @Autowired
    AdvertService advertService;
    @Autowired
    MediaClassifyService mediaClassifyService;

    /**
     * 媒体首页
     *
     * @return
     */
    @RequestMapping(value = "/index/{devicId}", method = RequestMethod.POST)
    @ResponseBody
    public String index(@PathVariable String devicId) {
        ResultObj resultObj = new ResultObj();
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> mediaList = new ArrayList<>();
        // List<Media> mediaList=mediaService.selectMediaListBydevicId(devicId);
        List<MediaClassify> mediaClassifyList = mediaClassifyService.getMediaClassify(new HashMap<String, String>()
          {
              {
                  put("parentId", "7");
              }
          }
        );
        for (MediaClassify mediaClassify :
                mediaClassifyList) {
            Map<String, String> m = new HashMap<>();
            m.put("devicId", devicId);
            m.put("classify", String.valueOf(mediaClassify.getTid()));
            m.put("status", String.valueOf(Media.STATUS_VALID));
            Map<String, Object> mediaClassifymap = new HashMap<>();
            mediaClassifymap.put("name", mediaClassify.getName());
            mediaClassifymap.put("data", mediaService.selectMediaList(m));
            mediaList.add(mediaClassifymap);
        }
        map.put("mediaList", mediaList);
        map.put("advertList", advertService.getAdvertBylocationId(1L));
        resultObj.setData(map);
        resultObj.setMsg("操作成功！");

        return resultObj.toJson();
    }

    /**
     * 获取媒体列表
     *
     * @return
     */
    @RequestMapping(value = "/getMediaList", method = RequestMethod.POST)
    @ResponseBody
    public String getMediaList(String token) {
        logger.info("getMediaList===>>" + token);
        ResultObj resultObj = new ResultObj();
        Map<String, String> map = super.showRequestParameters();
        System.out.println(super.request.getParameter("abc"));
        logger.info(map.toString());
        resultObj.setData(mediaService.getMediaList(map));
        resultObj.setMsg("操作成功！");

        return resultObj.toJson();
    }

    /**
     * 获取媒体详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String getMediaById(@PathVariable Long id) {
        logger.info("getMediaList===>>");
        ResultObj resultObj = new ResultObj();
        resultObj.setData(mediaService.getMediaById(id));
        return resultObj.toJson();
    }

    /**
     * 推送文件到手表
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/push/{devicId}/{id}/{memberId}", method = RequestMethod.POST)
    @ResponseBody
    public String pushMediaById(@PathVariable String devicId, @PathVariable Long id, @PathVariable String memberId) {
        logger.info("getMediaList===>>");
        ResultObj resultObj = new ResultObj();


        Media media = mediaService.pushMediaById(id, devicId, memberId);
        resultObj.setMsg("操作成功！");
        //TODO 发送到手表服务器,type=2表示是媒体PUSH
        String body = "{\"type\":2,\"devicId\":\"" + media.getDevicId() + "\",\"data\":" + JsonHandler.objectToJson(media) + "}";
        logger.info(body);
        PushTools.push2DevicService(devicId, body);
        return resultObj.toJson();
    }

}
